package main.java;
import org.jagatoo.logging.FileLog;
import org.jagatoo.logging.Log;
import org.jagatoo.logging.LogChannel;
import org.jagatoo.logging.LogLevel;
import org.xith3d.base.Xith3DEnvironment;
import org.xith3d.render.Canvas3D;
import org.xith3d.render.Canvas3DFactory;
import org.xith3d.render.config.OpenGLLayer;
import main.java.data.Configuration;
import main.java.scenes.Screen_Login;
import main.java.scenes.TestScene;

/**Manages the Client states
 * 
 * @author Sven Berger
 * 
 *
 */
public class StateManager 
{
	private final ResourceManager ress=new ResourceManager();
	private Configuration config=null;
	//private final MainLoop loop=null;
	private final Xith3DEnvironment env=new Xith3DEnvironment();
	private Screen_Login login=null;
	private final LogChannel logChannel = new LogChannel( "Aceituna Client" );
	private FileLog logfile=null;

	public StateManager()
	{
	}
	
	/**
	 * @throws Exception
	 */
	public void startUp() throws Exception
	{
		//if DEBUG
		this.logfile=new FileLog("Aeituna.log");
		//switch off to get all messages
		this.logfile.setChannelFilter(this.logChannel.getID());
		
		this.logfile.setLogLevel(LogLevel.DEBUG);
		this.ress.register_Log(this.logChannel);
		this.ress.setLogfile(this.logfile);
		
		//switch off to not save logmessage
		Log.getLogManager().registerLog(this.logfile);
		//switch off relative time messages
		Log.getLogManager().setTimestampingEnabled(true);
		
		this.ress.addMessag("Client started");
		
		this.config=this.ress.loadConfig();
		Canvas3D canvas=null;
		OpenGLLayer layer=null;
		switch ( this.config.getOpenGLID() )
	    {
			case 0:
				layer=OpenGLLayer.LWJGL_AWT;
				break;
			case 1:
				layer=OpenGLLayer.LWJGL;
				break;
			case 2:
				layer=OpenGLLayer.JOGL_AWT;
				break;
			case 3:
				layer=OpenGLLayer.JOGL_SWING;
				break;
			case 4:
				layer=OpenGLLayer.JOGL_SWT;
				break;
				
	    }
		canvas=Canvas3DFactory.createWindowed(layer,this.config.getResX(),this.config.getResY(), "Aceituna");
		this.env.addCanvas(canvas);
		MainLoop loop=new MainLoop(this.env,canvas, this.ress, new Screen_Login());
		loop.begin();
		
		
		//TestScene test= new TestScene(this.env, canvas, this.ress);
	}
	
}
